﻿using System;
using System.Diagnostics.CodeAnalysis;
using CmdTailCmd;

namespace regx
{
    enum ErrorLevelValue {Error = 0, NoError = 1, NoMatch = 1, Matched = 2}
    class ErrorLevel    
    {
        public ErrorLevelValue Value    {get; set;}
        public ErrorLevel() {Value = ErrorLevelValue.Error;}
    }
    static class ErrorLevelExtensions
    {
        public static void Set(this ErrorLevel lhs, ErrorLevelValue val) {if (lhs != null) lhs.Value = val;}
    }

    class Program
    {
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification="Top level catch to prevent a stack trace. We want *everything*")]
        static int Main(string[] args)
        {
            try
            {
                ErrorLevel el = new ErrorLevel();
                CmdTail tail = new CmdTail(args);

                if (DisplayHelp.Command(tail))                    return (int)ErrorLevelValue.NoError;
                if (tail.ExecuteIfValid<SanityCheckParams>(el))   return (int)el.Value;
                if (tail.ExecuteIfValid<SearchFileParams>(el))    return (int)el.Value;
                if (tail.ExecuteIfValid<SearchUrlParams>(el))     return (int)el.Value;
                if (tail.ExecuteIfValid<SearchFilterParams>(el))  return (int)el.Value;
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Unhandled error occured: " + e.Message);
            }
            
            return (int)ErrorLevelValue.Error;
        }
    }
}
